package com.itcast.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.itcast.entity.Result;
import com.itcast.mapper.MemberMapper;
import com.itcast.mapper.OrderMapper;
import com.itcast.mapper.ReportMapper;
import com.itcast.untils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service(interfaceClass = ReportService.class)
@Transactional
public class ReportServiceImpl implements ReportService{
    @Autowired
    private ReportMapper reportMapper;
    @Autowired
    private MemberMapper memberMapper;

    /**
     * 获取会员数量
     * @param dates
     * @return
     */
    @Override
    public List<Integer> find(List<String> dates) {
        List<Integer> memberCount = new ArrayList<>();
        for(String date:dates){
            String date1 = date +".31";
           Integer integer = reportMapper.find(date1);
           memberCount.add(integer);
        }
        return memberCount;
    }

    /**
     * 获取套餐占比
     * @return
     */
    @Override
    public Map<String, Object> getSetmealReport() {
       List<Map<String,Object>> list=  reportMapper.getSetmealReport();
       Map<String,Object> map1 = new HashMap<>();
       List<String> list1 = new ArrayList<>();
       for (Map<String,Object> map:list){
           list1.add((String)map.get("name"));
       }
       map1.put("setmealNames",list1);
       map1.put("setmealCount",list);
        return map1;
    }

    /**
     * 统计盈利数据
     * @return
     */
    @Override
    public Map<String,Object> getBusinessReportData() {
        try {
            //报表日期
            String today =  DateUtils.parseDate2String(new Date());
            //获得本周一日期
            String thisWeekMonday = DateUtils.parseDate2String(DateUtils.getThisWeekMonday());
            //获得本月第一天日期
            String firstDay4ThisMonth = DateUtils.parseDate2String(DateUtils.getFirstDay4ThisMonth());
            //本日新增会员数
            Integer todayNewMember = memberMapper.findMemberCountByDate(today);
            //总会员数
            Integer totalMember = memberMapper.findMemberTotalCount();
            //本周新增会员数
            Integer thisWeekNewMember = memberMapper.findMemberCountAfterDate(thisWeekMonday);
            //本月新增会员数
            Integer thisMonthNewMember = memberMapper.findMemberCountAfterDate(firstDay4ThisMonth);
            //今日预约数
            Integer todayOrderNumber = memberMapper.findOrderCountByDate(today);
            //本周预约数
            Integer thisWeekOrderNumber = memberMapper.findOrderCountAfterDate(thisWeekMonday);
            //本月预约数
            Integer thisMonthOrderNumber = memberMapper.findOrderCountAfterDate(firstDay4ThisMonth);
            //今日到诊数
            Integer todayVisitsNumber = memberMapper.findVisitsCountByDate(today);
            //本周到诊数
            Integer thisWeekVisitsNumber = memberMapper.findVisitsCountAfterDate(thisWeekMonday);
            //本月到诊数
            Integer thisMonthVisitsNumber = memberMapper.findVisitsCountAfterDate(firstDay4ThisMonth);
            //热门套餐查询
            List<Map> hotSetmeal = memberMapper.findHotSetmeal();
            Map<String,Object> result = new HashMap<>();
            result.put("reportDate",today);
            result.put("todayNewMember",todayNewMember);
            result.put("totalMember",totalMember);
            result.put("thisWeekNewMember",thisWeekNewMember);
            result.put("thisMonthNewMember",thisMonthNewMember);
            result.put("todayOrderNumber",todayOrderNumber);
            result.put("thisWeekOrderNumber",thisWeekOrderNumber);
            result.put("thisMonthOrderNumber",thisMonthOrderNumber);
            result.put("todayVisitsNumber",todayVisitsNumber);
            result.put("thisWeekVisitsNumber",thisWeekVisitsNumber);
            result.put("thisMonthVisitsNumber",thisMonthVisitsNumber);
            result.put("hotSetmeal",hotSetmeal);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}

